//201902104003邵潇漩
package bysj.service;

import bysj.dao.DepartmentDao;
import bysj.domain.Department;
import bysj.domain.School;
import bysj.exception.BysjException;
import util.Condition;
import util.JdbcHelper;
import util.Pagination;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;
/**
 * @author 邵潇漩
 * @version 1.0  11-13-2020
 */

public final class DepartmentService {
    private static DepartmentDao departmentDao= DepartmentDao.getInstance();
    private static DepartmentService departmentService=new DepartmentService();
    private DepartmentService(){}

    public static DepartmentService getInstance(){
        return departmentService;
    }

    public Collection<Department> findAll(Pagination pagination,List<Condition> conditionList) throws SQLException {
        //获得连接以便完成可能的事务操作
        Connection connection = JdbcHelper.getConn();
        Collection<Department> departments =null;
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        try {
            departments = departmentDao.findAll(pagination,conditionList, connection);
            //提交事务
            connection.commit();
        }catch (SQLException e) {
            //回滚事务中所有操作
            connection.rollback();
        } finally {
            //将自动提交设置为true，结束事务
            connection.setAutoCommit(true);
            //关闭连接
            JdbcHelper.close(connection);
        }
        return departments;
    }

    public Department find(Integer id) throws SQLException {
        //获得连接以便完成可能的事务操作
        Connection connection = JdbcHelper.getConn();
        Department department=null;
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        try {
            department=departmentDao.find(id,connection);
            //提交事务
            connection.commit();
        }catch (SQLException e) {
            //回滚事务中所有操作
            connection.rollback();
        } finally {
            //将自动提交设置为true，结束事务
            connection.setAutoCommit(true);
            //关闭连接
            JdbcHelper.close(connection);
        }
        return department;
    }

    public boolean update(Department department) throws SQLException {
        //获得连接以便完成可能的事务操作
        Connection connection = JdbcHelper.getConn();
        boolean dept_update=false;
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        try {
            dept_update=departmentDao.update(department,connection);
            //提交事务
            connection.commit();
        }catch (SQLException e) {
            //回滚事务中所有操作
            connection.rollback();
        } finally {
            //将自动提交设置为true，结束事务
            connection.setAutoCommit(true);
            //关闭连接
            JdbcHelper.close(connection);
        }
        return dept_update;
    }

    public boolean add(Department department) throws SQLException {
        //获得连接以便完成可能的事务操作
        Connection connection = JdbcHelper.getConn();
        boolean dept_add=false;
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        try {
            dept_add=departmentDao.add(department,connection);
            //提交事务
            connection.commit();
        }catch (SQLException e) {
            //回滚事务中所有操作
            connection.rollback();
        } finally {
            //将自动提交设置为true，结束事务
            connection.setAutoCommit(true);
            //关闭连接
            JdbcHelper.close(connection);
        }
        return dept_add;
    }

    public boolean delete(Integer id) throws SQLException,BysjException {
        //获得连接
        Connection connection = JdbcHelper.getConn();
        Department department=this.find(id);
        //获得所有下一级单位(Department)的数量
        int countTeachersOfDepartment = TeacherService.getInstance().countAll(connection,department);
        boolean dept_del = false;
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        try {
            //若没有二级单位，则能够删除，否则抛出异常
            if (countTeachersOfDepartment == 0) {
                dept_del = departmentDao.delete( id,connection);
            }else {
                JdbcHelper.close(connection);
                throw new BysjException(department.getDescription() + "仍然有下属教师，不能删除");
            }
            //提交事务
            connection.commit();
        }catch (SQLException e) {
            //回滚事务中所有操作
            connection.rollback();
        } finally {
            //将自动提交设置为true，结束事务
            connection.setAutoCommit(true);
            //关闭连接
            JdbcHelper.close(connection);
        }
        return dept_del;
    }

    public int countAll(Connection connection,School school) throws SQLException{
        return departmentDao.countAll(school,connection);
    }
}

